// src/services/api.js
import axios from 'axios'

const siliconFlow = {
		apiKey: 'sk-jammjrdzakqbpehfkphffllpxxwsykgwxqvovhhgrjdoyqyb', // 硅基流动API密钥
		apiBase: 'https://api.siliconflow.cn/v1', // 硅基流动API基础URL
		model: 'deepseek-ai/DeepSeek-V3', // 使用的模型
	}

// GitLab API 实例
export const gitlabApi = axios.create({
  baseURL: localStorage.getItem('gitlab_base_url') || '',
  timeout: 30000
})

// DeepSeek API 实例
export const deepseekApi = axios.create({
  baseURL: siliconFlow.apiBase,
  timeout: 30000
})

// 请求拦截器
gitlabApi.interceptors.request.use((config) => {
  const token = localStorage.getItem('gitlab_access_token')
  if (token) {
    config.headers['PRIVATE-TOKEN'] = token
  }
  return config
})

deepseekApi.interceptors.request.use((config) => {
  const apiKey = 'sk-jammjrdzakqbpehfkphffllpxxwsykgwxqvovhhgrjdoyqyb'
  if (apiKey) {
    config.headers['Authorization'] = `Bearer ${siliconFlow.apiKey}`
  }
  return config
})

// 更新配置
export const updateApiConfig = {
  gitlab: (config) => {
    gitlabApi.defaults.baseURL = config.baseUrl
    localStorage.setItem('gitlab_base_url', config.baseUrl)
    localStorage.setItem('gitlab_access_token', config.accessToken)
  },
  deepseek: (apiKey) => {
    localStorage.setItem('deepseek_api_key', apiKey)
  }
}